﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Entities.BaseClasses;
using System.Collections.Generic;
namespace SMS.ViewModel.Admin
{
    public class DeActivateUserViewModel:BaseNotify
    {
        SMS.Model.AdminModel _model;
        public DeActivateUserViewModel()
        {
            if (App.IsRunning)
            {
                _model = new Model.AdminModel();
                _model.ActivateDeActivateUserLoginCompleted +=new Action<int>(_model_ActivateDeActivateUserLoginCompleted);
                _model.ManageUserRoleSettingCompleted += new Action<int>(_model_ManageUserRoleSettingCompleted);
                _model.OnServiceError += _model_OnServiceError;
                ActivateDeActivateUserCommand = new Common.Silverlight.DelegateCommand(ActivateDeActivate);
                CancelCommand = new SMS.Common.Silverlight.DelegateCommand(Cancel);
                AccessRoleList = new List<RoleType>()
                {
                    new RoleType { RoleValue=1,RoleName="Registered User" },
                    new RoleType{RoleValue=2,RoleName="Admin User"},
                    new RoleType{RoleValue=3,RoleName="Super User"}
                };
                
            }
        }

        void _model_ManageUserRoleSettingCompleted(int obj)
        {
            if (obj > 0)
            {
                UserMessage = "User access role changed successfully";
            }
            else
            {
                ErrorMessage = "Error Occured!!!";
            }
        }

        void _model_OnServiceError(Entities.SMSException obj)
        {
            if(obj!=null)
                ErrorMessage = obj.Message;
        }

        void _model_ActivateDeActivateUserLoginCompleted(int obj)
        {
            if (obj > 0 && ActivateUser)
            {
                UserMessage = "User Activated Successfully";
            }
            else if (obj > 0 && DeActivateUser)
            {
                UserMessage = "User De-Activated Successfully";
            }
            else
            {
                ErrorMessage = "Error Occured!!";
            }
        }

        private void ActivateDeActivate(object param)
        {
            if (SelectedTabIndex == 0)
            {
                if (ActivateUser)
                {
                    _model.ActivateDeActivateUserLogin(SelectedUser.UserName, "ACTIVATE");
                }
                else if (DeActivateUser)
                {
                    _model.ActivateDeActivateUserLogin(SelectedUser.UserName, "DEACTIVATE");
                }
            }
            else if (SelectedTabIndex == 1)
            {
                _model.ManageUserRoleSetting(SelectedUser.UserName, SelectedUserRole.RoleValue); 
            }
        }

        private void Cancel(object param)
        {
            App.GoBack();
        }

        private bool _ActivateUser=true;

        public bool ActivateUser
        {
            get { return _ActivateUser; }
            set { _ActivateUser = value; OnPropertyChanged("ActivateUser"); }
        }

        private bool _DeActivateUser;

        public bool DeActivateUser
        {
            get { return _DeActivateUser; }
            set { _DeActivateUser = value; OnPropertyChanged("DeActivateUser"); }
        }

        private SMS.Entities.Admin.ApplicationUser  _SelectedUser;

        public SMS.Entities.Admin.ApplicationUser  SelectedUser
        {
            get { return _SelectedUser; }
            set { _SelectedUser = value; OnPropertyChanged("SelectedUser"); }
        }

        private int _SelectedTabIndex=0;

        public int SelectedTabIndex
        {
            get { return _SelectedTabIndex; }
            set 
            {
                _SelectedTabIndex = value; 
                OnPropertyChanged("SelectedTabIndex"); 
            }
        }

        private List<RoleType> _AccessRoleList;

        public List<RoleType> AccessRoleList
        {
            get { return _AccessRoleList; }
            set { _AccessRoleList = value; OnPropertyChanged("AccessRoleList"); }
        }

        private RoleType _SelectedUserRole;

        public RoleType SelectedUserRole
        {
            get { return _SelectedUserRole; }
            set { _SelectedUserRole = value; OnPropertyChanged("SelectedUserRole"); }
        }
        
        
        public ICommand ActivateDeActivateUserCommand { get; set; }
        public ICommand CancelCommand { get; set; }
    }

    public class RoleType
    {
        public int RoleValue { get; set; }
        public string RoleName { get; set; }
    }
}
